package com.ylz.sjzx.sio.common.core.core.container;

import com.ylz.sjzx.sio.common.core.core.Interceptor.IInterceptor;
import com.ylz.sjzx.sio.common.core.core.container.common.BaseScanner;
import com.ylz.sjzx.sio.common.core.core.container.common.annotation.InterceptorDesc;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 过滤器和拦截器扫描器
 */
public class ChainScanner extends BaseScanner {

    /**
     * 扫描Filter组件，Filter必须有AdapterDesc注解，否则忽略。
     *
     * @return List \\<{@link IInterceptor} \\> 扫描到的class根目录下的所有{@link IInterceptor} 类实现。
     */
    public static List<IInterceptor> scanInterceptor() {
        List<IInterceptor> interceptors = new ArrayList<>();
        Set<Class<?>> classSet = BaseScanner.scan(IInterceptor.class);
        if (classSet != null) {
            for (Class<?> classObj : classSet) {
                InterceptorDesc config = classObj.getAnnotation(InterceptorDesc.class);
                if (config != null) {
                    try {
                        interceptors.add(IInterceptor.class.cast(classObj.newInstance()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return interceptors;
    }

}
